package com.example.oppo;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.oppo.utils.LogUtils;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    public static final String TAG="MainActivuty";
    private LinearLayout mlayout;
  private  TextView textView1;
  private  TextView textView2;
  private  TextView textView3;
  private TextView textivew4;
  private Button mbutton;
  private EditText editText;
  private ImageView imageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;//hello
        });
        mlayout=findViewById(R.id.main);
        textView1 = findViewById(R.id.textivew1);
        textView2 = findViewById(R.id.textivew2);
        textView3 = findViewById(R.id.textivew3);
        textivew4 = findViewById(R.id.textivew4);

        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
//                startActivity(intent);
                //showToast("123");
                LogUtils.d(TAG,"=====mTextView1 onClick=====");
                onTextViewClick(1);
            }
        });
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
//                startActivity(intent);
                LogUtils.d(TAG,"=====mTextView2 onClick=====");
                onTextViewClick(2);

            }
        });
        textivew4.setText("直接写字符串");
        //textView4.setText(R.string.app_name123);
//        textivew4.setText(getResources().getString(R.string.app_name123));
        textivew4.setVisibility(View.VISIBLE);
        //textView1.setVisibility(View.GONE);
        textView3.setVisibility(View.VISIBLE);
        textView3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                onTextViewClick(3);
            }
        });
        mlayout.setOnClickListener(this);
        //textView4.setBackground(getDrawable(R.mipmap.aaa));
        //textView4.setBackgroundResource(R.color.black);
        //textView4.setTextSize(123);
        textivew4.setTextSize(getResources().getDimension(R.dimen.text_size));
        textivew4.setTextColor(getColor(R.color.black));

        LogUtils.d(TAG,"textView4显示的文字是："+textivew4.getText());
        textivew4.setOnClickListener(this);



        mbutton=findViewById(R.id.botton);
        mbutton.setBackground(getDrawable(R.drawable.button_bg_seletor));
        //mbutton.setBackgroundResource(R.mipmap.orange);
        mbutton.setTextSize(getResources().getDimension(R.dimen.text_size));
        //mbutton.setTextColor(getColor(R.color.black));
        mbutton.setOnClickListener(this);

        editText=findViewById(R.id.edit);
        editText.setText("1234567");
        editText.requestFocus();
        editText.setSelection(2);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                LogUtils.d(TAG,"beforeTextchange:"+s.toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LogUtils.d(TAG,"onTextchange:"+s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
                LogUtils.d(TAG,"afterTextchange:"+s.toString());
                textView1.setText(s.toString());
            }
        });

        imageView=findViewById(R.id.image);
        imageView.setImageDrawable(getDrawable(R.mipmap.blue));
        imageView.setImageResource(R.mipmap.aaa);
        imageView.setBackground(getDrawable(R.mipmap.red));
        imageView.setBackgroundResource(R.drawable.button_bg_seletor);
        imageView.setOnClickListener(this);


    }
    private void onTextViewClick(int i){
        LogUtils.d(TAG,"====onTextViewClick===="+i);
        if (i==1){
            showToast("TextView 1 Click");
        }else if (i==2){
            showActivity();
        }else if (i==3){
            showToast("TextView 3 Click");
        }
    }
    private void showToast(String text){
//        Log.d("test111","*******"+text);
        LogUtils.d(TAG,"=====showToast======"+text);
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
    private void showActivity(){
        LogUtils.d(TAG,"=====showActivity======");
        Intent intent=new Intent(MainActivity.this,SecondActivity.class);
        startActivity(intent);
    }

    @Override
    public void onClick(View v){
        int id=v.getId();
        if (id==R.id.botton) {
            LogUtils.d(TAG, "Button click");
        } else if (id == R.id.textivew4) {
            LogUtils.d(TAG, "TextView4 click");
        } else if (id == R.id.main) {
            LogUtils.d(TAG, "Main click");
        } else if (id==R.id.image) {
            LogUtils.d(TAG, "ImageView click");
        }
    }
}